/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.editors;

import com.eng.scc.core.data.Company;
import com.eng.scc.core.db.manager.service.FuelControllerManagerService;
import com.eng.scc.core.parts.CompaniesList;
import com.eng.scc.core.parts.DefaultPicker;
import com.eng.scc.core.parts.Picker;
import com.eng.scc.core.view.ApplicationContext;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JOptionPane;

/**
 *
 * @author Rafael
 */
public class CompanyPicker extends javax.swing.JPanel implements Picker<Company>
{
    public static Company pickClient( Component owner )
    {
        CompanyPicker companyPicker = new CompanyPicker();
        
        return DefaultPicker.pick( owner, "Selecione uma Empresa", companyPicker );
    }
    
    public CompanyPicker()
    {
        initComponents();
        
        try
        {
            listCompanies.setCompanies( FuelControllerManagerService.getInstance().getCompanies() );
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    @Override
    public Company getSelectedValue()
    {
        return listCompanies.getSelectedCompany();
    }

    @Override
    public boolean validatePicker()
    {
        if ( listCompanies.getSelectedCompany() == null )
        {
            JOptionPane.showMessageDialog( this, "Informe uma Empresa!" );
            return false;
        }
        
        return true;
    }

    @Override
    public JComponent getComponent()
    {
        return this;
    }

    private void initComponents()
    {
        setLayout( new GridBagLayout() );
        add( listCompanies, 
             new GridBagConstraints( 0, 0, 1, 1, 1, 1, 
                                        GridBagConstraints.CENTER, 
                                        GridBagConstraints.BOTH, 
                                        new Insets( 5, 5, 5, 5 ), 0, 0 ) );
    }
    
    private CompaniesList listCompanies = new CompaniesList();
}
